package com.carkeeper.order.service.impl;

import com.carkeeper.entity.Result;
import com.carkeeper.order.dao.OrderDetailMapper;
import com.carkeeper.order.dao.OrderLogMapper;
import com.carkeeper.order.dao.OrderMapper;
import com.carkeeper.order.pojo.*;
import com.carkeeper.order.service.OrderService;
import com.carkeeper.shop.feign.StoreFeign;
import com.carkeeper.shop.pojo.Store;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderLogMapper orderLogMapper;


    @Autowired
    private StoreFeign storeFeign;

    //分页查询
    @Override
    public Page<Order> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Order>)orderMapper.selectAll();
    }

    //根据id查询详细订单
    public OrderGroup findByid(String id) {
        OrderGroup orderGroup=new OrderGroup();
        Order order = orderMapper.selectByPrimaryKey(id);
        //添加order对象
        orderGroup.setOrder(order);
        OrderDetail orderDetail=new OrderDetail();
        orderDetail.setOrderId(id);
        List<OrderDetail> orderDetails= orderDetailMapper.select(orderDetail);
        //添加orderdetail对象
        orderGroup.setOrderDetailList(orderDetails);
        OrderLog orderLog=new OrderLog();
        orderLog.setOrderId(id);
        List<OrderLog> orderLogList = orderLogMapper.select(orderLog);
        Result<Store> storeById = storeFeign.findStoreById(order.getStoreId());
        Store store = storeById.getData();
        //添加orderlog对象
        orderGroup.setOrderLogs(orderLogList);
        //添加商店表实体对象
        orderGroup.setStore(store);
        return orderGroup;


    }

  //条件分页查询
    @Override
    public Page<Order> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
       return (Page<Order>) orderMapper.selectByExample(example);
    }

    //根据单个id 查询Order
    @Override
    public Order findByOrderId(String id) {
        return orderMapper.selectByPrimaryKey(id);
    }


    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String,Object>searchMap){
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap!=null){
            // 订单编号
            if (searchMap.get("code")!=null && !"".equals(searchMap.get("code"))){
                  criteria.andEqualTo("code",searchMap.get("code"));
            }
            // 支付类型
            if (searchMap.get("payStatus")!=null &&!"".equals(searchMap.get("payStatus"))){
                criteria.andEqualTo("payStatus",searchMap.get("payStatus"));
            }
            //订单状态
            if (searchMap.get("orderStatus")!=null&&!"".equals(searchMap.get("orderStatus"))){
                criteria.andEqualTo("orderStatus",searchMap.get("orderStatus"));
            }
            //商家电话
            if (searchMap.get("storeTel")!=null &&!"".equals(searchMap.get("storeTel"))){
                criteria.andEqualTo("storeTel",searchMap.get("storeTel"));
            }
            //客户名字
            if (searchMap.get("contact")!=null &&!"".equals(searchMap.get("contact"))){
                criteria.andEqualTo("contact",searchMap.get("contact"));
            }
            //店铺名称
            if (searchMap.get("storeName")!=null&&!"".equals(searchMap.get("storeName"))){
                 criteria.andEqualTo("storeName",searchMap.get("storeName"));
            }
            //订单时间
            if (searchMap.get("createDate")!=null&&!"".equals(searchMap.get("createDate"))){
                criteria.andEqualTo("createDate",searchMap.get("createDate"));
            }
        }
        return example;
    }

}
